package com.pi.pro.xcmg.am.was.web.xwms.controller;


import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.pi.common.utils.LogSupport;
import com.pi.common.utils.TextUtils;
import com.pi.pro.xcmg.am.common.XcmgAmConstants;
import com.pi.pro.xcmg.am.was.edi.infor.common.InforWasExchangeSupport;
import com.pi.pro.xcmg.am.was.edi.infor.common.InforWasXcmgExchangeConstants;
import com.pi.pro.xcmg.am.was.edi.infor.dto.reply.*;
import com.pi.pro.xcmg.am.was.edi.infor.dto.req.FuncRequestData;
import com.pi.pro.xcmg.am.was.edi.infor.dto.req.FuncRequestListData;
import com.pi.pro.xcmg.am.was.edi.infor.dto.req.StorageQueryData;
import com.pi.pro.xcmg.am.was.edi.infor.dto.req.TaskReportData;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;


/**
 * 模拟数据
 */
@Slf4j
@RestController
@RequestMapping("/api/was/")
public class XwmsMockApiController {

    protected Logger ioLogger= LogSupport.getIoLogger();

    private Cache<String, String> containNumCache= CacheBuilder.newBuilder().expireAfterWrite(10, TimeUnit.MINUTES).build();

    @PostMapping(value="/storage/query")
    public FuncReplyData<StorageLineResultDto> query(@RequestBody StorageQueryData queryData){
        //
        FuncReplyData<StorageLineResultDto> storageData = InforWasExchangeSupport.emptyStorageResultReply();
        log.debug("ApiController XwmsMockApiController inventory query://{}",queryData);
        ioLogger.debug(">>>> [STORAGE-QUERY] //{}",queryData);

        storageData.setResultData(this.mockStorages());
        return storageData;
    }



    //[OK]
    @PostMapping(value="/task/report")
    public String report(@RequestBody FuncRequestData<TaskReportData> reportData){
        //
        FuncReplyData<TaskReportResultDto> resultData = InforWasExchangeSupport.emptyTaskReportResultReply();
        log.debug("ApiController XwmsMockApiController task report://{}", reportData);
        ioLogger.debug(">>>> [TASK-REPORT] //{}", reportData);

        resultData.setResultData(this.mockTaskReportResults(reportData));

        String replyText = "[{\"projectCode\":\"XCMG\",\"processCode\":\"P_WCS2WMS_TASK_RESULT\",\"processRunCode\":\"P_WCS2WMS_TASK_RESULT$$77924061-3984-44c1-9e62-36defa1879e9\",\"resultData\":{\"__PROCESS_OUTPUT__\":{\"resultcode\":\"403\",\"message\":\"在徐工WMS中未找到足够的可用库存\"}},\"errors\":[],\"warnErrors\":null,\"processName\":\"任务执行结果反馈接口（立库）\",\"runCode\":\"P_WCS2WMS_TASK_RESULT$$77924061-3984-44c1-9e62-36defa1879e9\",\"startTime\":1747127923897,\"endTime\":1747127923937,\"userId\":-1,\"userName\":\"eaiSystem\",\"longRun\":false,\"asyncProcessTaskId\":null}]";
        //replyText = "[{\"projectCode\":\"XCMG\",\"processCode\":\"P_WCS2WMS_TASK_RESULT\",\"processRunCode\":\"P_WCS2WMS_TASK_RESULT$$77924061-3984-44c1-9e62-36defa1879e9\",\"resultData\":{\"__PROCESS_OUTPUT__\":{\"resultcode\":\"403\",\"message\":\"在徐工WMS中未找到足够的可用库存\"}},\"errors\":[\"可用库存不足\"],\"warnErrors\":null,\"processName\":\"任务执行结果反馈接口（立库）\",\"runCode\":\"P_WCS2WMS_TASK_RESULT$$77924061-3984-44c1-9e62-36defa1879e9\",\"startTime\":1747127923897,\"endTime\":1747127923937,\"userId\":-1,\"userName\":\"eaiSystem\",\"longRun\":false,\"asyncProcessTaskId\":null}]";
        //replyText = "{\"projectCode\":\"XCMG\",\"processCode\":\"P_WCS2WMS_TASK_RESULT\",\"processRunCode\":\"P_WCS2WMS_TASK_RESULT$$77924061-3984-44c1-9e62-36defa1879e9\",\"resultData\":{\"__PROCESS_OUTPUT__\":{\"resultcode\":\"403\",\"message\":\"在徐工WMS中未找到足够的可用库存\"}},\"errors\":[],\"warnErrors\":null,\"processName\":\"任务执行结果反馈接口（立库）\",\"runCode\":\"P_WCS2WMS_TASK_RESULT$$77924061-3984-44c1-9e62-36defa1879e9\",\"startTime\":1747127923897,\"endTime\":1747127923937,\"userId\":-1,\"userName\":\"eaiSystem\",\"longRun\":false,\"asyncProcessTaskId\":null}";
        //String replyText = "[{\"projectCode\":\"XCMG\",\"processCode\":\"P_WCS2WMS_TASK_RESULT\",\"processRunCode\":\"P_WCS2WMS_TASK_RESULT$$77924061-3984-44c1-9e62-36defa1879e9\",\"resultData\":{\"__PROCESS_OUTPUT__\":{\"resultcode\":\"403\",\"message\":\"在徐工WMS中未找到足够的可用库存\"}},\"errors\":[],\"warnErrors\":\"ERRO-CHECK\",\"processName\":\"任务执行结果反馈接口（立库）\",\"runCode\":\"P_WCS2WMS_TASK_RESULT$$77924061-3984-44c1-9e62-36defa1879e9\",\"startTime\":1747127923897,\"endTime\":1747127923937,\"userId\":-1,\"userName\":\"eaiSystem\",\"longRun\":false,\"asyncProcessTaskId\":null}]";
        try{

            resultData= InforWasExchangeSupport.parseJson(replyText, TaskReportResultDto.class);//.parseJson(replyText, TaskReportResultDto.class);
        }catch (Exception e){
            e.printStackTrace();
        }
        return replyText;
    }

/**
    [OK]
    @PostMapping(value="/task/report")
    public FuncReplyData<TaskReportResultDto> report(@RequestBody FuncRequestData<TaskReportData> reportData){
        //
        FuncReplyData<TaskReportResultDto> resultData = InforWasExchangeSupport.emptyTaskReportResultReply();
        log.debug("ApiController XwmsMockApiController task report://{}", reportData);
        ioLogger.debug(">>>> [TASK-REPORT] //{}", reportData);

        resultData.setResultData(this.mockTaskReportResults(reportData));

        String replyText = "[{\"projectCode\":\"XCMG\",\"processCode\":\"P_WCS2WMS_TASK_RESULT\",\"processRunCode\":\"P_WCS2WMS_TASK_RESULT$$77924061-3984-44c1-9e62-36defa1879e9\",\"resultData\":{\"__PROCESS_OUTPUT__\":{\"resultcode\":\"403\",\"message\":\"在徐工WMS中未找到足够的可用库存\"}},\"errors\":[],\"warnErrors\":null,\"processName\":\"任务执行结果反馈接口（立库）\",\"runCode\":\"P_WCS2WMS_TASK_RESULT$$77924061-3984-44c1-9e62-36defa1879e9\",\"startTime\":1747127923897,\"endTime\":1747127923937,\"userId\":-1,\"userName\":\"eaiSystem\",\"longRun\":false,\"asyncProcessTaskId\":null}]";
        replyText = "{\"projectCode\":\"XCMG\",\"processCode\":\"P_WCS2WMS_TASK_RESULT\",\"processRunCode\":\"P_WCS2WMS_TASK_RESULT$$77924061-3984-44c1-9e62-36defa1879e9\",\"resultData\":{\"__PROCESS_OUTPUT__\":{\"resultcode\":\"403\",\"message\":\"在徐工WMS中未找到足够的可用库存\"}},\"errors\":[],\"warnErrors\":null,\"processName\":\"任务执行结果反馈接口（立库）\",\"runCode\":\"P_WCS2WMS_TASK_RESULT$$77924061-3984-44c1-9e62-36defa1879e9\",\"startTime\":1747127923897,\"endTime\":1747127923937,\"userId\":-1,\"userName\":\"eaiSystem\",\"longRun\":false,\"asyncProcessTaskId\":null}";
        try{

            resultData= InforWasExchangeSupport.parseJson(replyText, TaskReportResultDto.class);//.parseJson(replyText, TaskReportResultDto.class);
        }catch (Exception e){
            e.printStackTrace();
        }
        return resultData;
    }
**/



    /**
    @PostMapping(value="/task/report")
    public FuncReplyData<TaskReportResultDto> report(@RequestBody FuncRequestData<TaskReportData> reportData){
        //
        FuncReplyData<TaskReportResultDto> resultData = InforWasExchangeSupport.emptyTaskReportResultReply();
        log.debug("ApiController XwmsMockApiController task report://{}", reportData);
        ioLogger.debug(">>>> [TASK-REPORT] //{}", reportData);

        resultData.setResultData(this.mockTaskReportResults(reportData));

        String replyText = "[{\"projectCode\":\"XCMG\",\"processCode\":\"P_WCS2WMS_TASK_RESULT\",\"processRunCode\":\"P_WCS2WMS_TASK_RESULT$$77924061-3984-44c1-9e62-36defa1879e9\",\"resultData\":{\"__PROCESS_OUTPUT__\":{\"resultcode\":\"403\",\"message\":\"在徐工WMS中未找到足够的可用库存\"}},\"errors\":[],\"warnErrors\":null,\"processName\":\"任务执行结果反馈接口（立库）\",\"runCode\":\"P_WCS2WMS_TASK_RESULT$$77924061-3984-44c1-9e62-36defa1879e9\",\"startTime\":1747127923897,\"endTime\":1747127923937,\"userId\":-1,\"userName\":\"eaiSystem\",\"longRun\":false,\"asyncProcessTaskId\":null}]";
        //replyText = "{\"projectCode\":\"XCMG\",\"processCode\":\"P_WCS2WMS_TASK_RESULT\",\"processRunCode\":\"P_WCS2WMS_TASK_RESULT$$77924061-3984-44c1-9e62-36defa1879e9\",\"resultData\":{\"__PROCESS_OUTPUT__\":{\"resultcode\":\"403\",\"message\":\"在徐工WMS中未找到足够的可用库存\"}},\"errors\":[],\"warnErrors\":null,\"processName\":\"任务执行结果反馈接口（立库）\",\"runCode\":\"P_WCS2WMS_TASK_RESULT$$77924061-3984-44c1-9e62-36defa1879e9\",\"startTime\":1747127923897,\"endTime\":1747127923937,\"userId\":-1,\"userName\":\"eaiSystem\",\"longRun\":false,\"asyncProcessTaskId\":null}";
        try{
            resultData= InforWasExchangeSupport.parseJson(replyText, TaskReportResultDto.class);
        }catch (Exception e){
            e.printStackTrace();
        }
        return resultData;
    }
    **/
    /**

    @PostMapping(value="/task/report")
    public String report(@RequestBody FuncRequestListData<TaskReportData> reportData){
        //
        String resultDataText="[{\"projectCode\":\"XCMG\",\"processCode\":\"P_WCS2WMS_TASK_RESULT\",\"processRunCode\":\"P_WCS2WMS_TASK_RESULT$$8aa8a69c-846b-4309-ad63-1cab05be17bb\",\"resultData\":{\"__PROCESS_OUTPUT__\":{\"resultcode\":\"100\",\"message\":\"处理成功\"}},\"errors\":[],\"warnErrors\":null,\"processName\":\"任务执行结果反馈接口（立库）\",\"runCode\":\"P_WCS2WMS_TASK_RESULT$$8aa8a69c-846b-4309-ad63-1cab05be17bb\",\"startTime\":1747127923118,\"endTime\":1747127923441,\"userId\":-1,\"userName\":\"eaiSystem\",\"longRun\":false,\"asyncProcessTaskId\":null}]";
        resultDataText="{\"projectCode\":\"XCMG\",\"processCode\":\"P_WCS2WMS_TASK_RESULT\",\"processRunCode\":\"P_WCS2WMS_TASK_RESULT$$8aa8a69c-846b-4309-ad63-1cab05be17bb\",\"resultData\":{\"__PROCESS_OUTPUT__\":{\"resultcode\":\"100\",\"message\":\"处理成功\"}},\"errors\":[],\"warnErrors\":null,\"processName\":\"任务执行结果反馈接口（立库）\",\"runCode\":\"P_WCS2WMS_TASK_RESULT$$8aa8a69c-846b-4309-ad63-1cab05be17bb\",\"startTime\":1747127923118,\"endTime\":1747127923441,\"userId\":-1,\"userName\":\"eaiSystem\",\"longRun\":false,\"asyncProcessTaskId\":null}";
        return resultDataText;
    }
 **/


    private FuncReplyResultsDto<StorageLineResultDto> mockStorages(){
        FuncReplyResultsDto resultOutputData = new FuncReplyResultsDto();
        List<StorageLineResultDto> lines = new ArrayList<>();
        StorageLineResultDto line = new StorageLineResultDto();
        line.setSku("470402576X020");
        line.setDescr("T309.201.0103 轮辋螺栓");
        line.setQty(new BigDecimal("200"));
        line.setStorerkey(XcmgAmConstants.CUSTOMER_CODE_XCMG_AM);
        //WBS
        line.setLottable01("T202306-XT804-20/976");
        //工厂库存地
        line.setLottable02("22901301");
        //供应商代码
        line.setLottable03("116923");
        //WMS批
        line.setLot("00002227510020");
        //收货日期,YYYY-MM-DD
        line.setLottable04("2025-03-23");
        //WMS库位编码
        line.setLoc("LK01S");
        lines.add(line);

        //
        line = new StorageLineResultDto();
        line.setSku("470402576X020");
        line.setDescr("T309.201.0103 轮辋螺栓");
        line.setQty(new BigDecimal("100"));
        line.setStorerkey(XcmgAmConstants.CUSTOMER_CODE_XCMG_AM);
        //WBS
        line.setLottable01("T202306-XT804-20/976");
        //工厂库存地
        line.setLottable02("22901301");
        //供应商代码
        line.setLottable03("116923");
        //WMS批
        line.setLot("00002227610010");
        //收货日期,YYYY-MM-DD
        line.setLottable04("2025-03-25");
        //WMS库位编码
        line.setLoc("LK01S");
        lines.add(line);

        //
        line = new StorageLineResultDto();
        line.setSku("470100374");
        line.setDescr("T105.001.0400 拖挂销总成");
        line.setQty(new BigDecimal("500"));
        line.setStorerkey(XcmgAmConstants.CUSTOMER_CODE_XCMG_AM);
        //WBS
        line.setLottable01("T202306-XT804-30/976");
        //工厂库存地
        line.setLottable02("22901301");
        //供应商代码
        line.setLottable03("116923");
        //WMS批
        line.setLot("0000222761");
        //收货日期,YYYY-MM-DD
        line.setLottable04("2025-03-25");
        //WMS库位编码
        line.setLoc("LK01S");
        lines.add(line);

        //
        line = new StorageLineResultDto();
        line.setSku("470100374");
        line.setDescr("T105.001.0400 拖挂销总成");
        line.setQty(new BigDecimal("700"));
        line.setStorerkey(XcmgAmConstants.CUSTOMER_CODE_XCMG_AM);
        //WBS
        line.setLottable01("T202306-XT804-30/976");
        //工厂库存地
        line.setLottable02("22901301");
        //供应商代码
        line.setLottable03("108849");
        //WMS批
        line.setLot("0000222761");
        //收货日期,YYYY-MM-DD
        line.setLottable04("2025-03-25");
        //WMS库位编码
        line.setLoc("LK01S");
        lines.add(line);

        line = new StorageLineResultDto();
        line.setSku("470208132");
        line.setDescr("T508.130.1300 气管总成");
        line.setQty(new BigDecimal("200"));
        line.setStorerkey(XcmgAmConstants.CUSTOMER_CODE_XCMG_AM);
        //WBS
        line.setLottable01("T202406-XT1104-20/76");
        //工厂库存地
        line.setLottable02("22901351");
        //供应商代码
        line.setLottable03("108849");
        //WMS批
        line.setLot("0000233751");
        //收货日期,YYYY-MM-DD
        line.setLottable04("2025-04-23");
        //WMS库位编码
        line.setLoc("LK01S");
        lines.add(line);

        resultOutputData.setProcessOutputs(lines);

        return resultOutputData;
    }


    private FuncReplyResultsDto<TaskReportResultDto> mockTaskReportResults(FuncRequestListData<TaskReportData> reportData){
        FuncReplyResultsDto resultOutputData = new FuncReplyResultsDto();
        List<TaskReportResultDto> lines = new ArrayList<>();
        TaskReportResultDto line = null;

        for (TaskReportData t:reportData.getData()){
            if (t==null){
                continue;
            }
            line = new TaskReportResultDto();
            line.setTaskkey(t.getTaskkey());
            line.setType(t.getType());
            line.setLot(t.getLot());
            line.setLoc(t.getToloc());
            line.setSku(t.getSku());
            line.setResultcode(InforWasXcmgExchangeConstants.RESULT_CODE_OK);
            line.setMessage("成功");
            lines.add(line);
        }
        resultOutputData.setProcessOutputs(lines);

        return resultOutputData;
    }

    private FuncReplyResultsDto<TaskReportResultDto> mockTaskReportResults(FuncRequestData<TaskReportData> reportData){
        FuncReplyResultsDto resultOutputData = new FuncReplyResultsDto();
        List<TaskReportResultDto> lines = new ArrayList<>();
        TaskReportResultDto line = null;

        TaskReportData t = reportData.getData();
            line = new TaskReportResultDto();
            line.setTaskkey(t.getTaskkey());
            line.setType(t.getType());
            line.setLot(t.getLot());
            line.setLoc(t.getToloc());
            line.setSku(t.getSku());
            line.setResultcode(InforWasXcmgExchangeConstants.RESULT_CODE_OK);
            line.setMessage("成功");
            lines.add(line);
        resultOutputData.setProcessOutputs(lines);

        return resultOutputData;
    }

    private FuncReplyResultsDto<TaskReportResultDto> mockTaskReportErrorResults(FuncRequestData<TaskReportData> reportData){
        FuncReplyResultsDto resultOutputData = new FuncReplyResultsDto();
        List<TaskReportResultDto> lines = new ArrayList<>();
        TaskReportResultDto line = null;

        TaskReportData t = reportData.getData();
        line = new TaskReportResultDto();
        line.setTaskkey(t.getTaskkey());
        line.setType(t.getType());
        line.setLot(t.getLot());
        line.setLoc(t.getToloc());
        line.setSku(t.getSku());
        line.setResultcode(InforWasXcmgExchangeConstants.RESULT_CODE_OK);
        line.setMessage("成功");
        lines.add(line);
        resultOutputData.setProcessOutputs(lines);

        return resultOutputData;
    }
}
